package com.account.boot.service.admin.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.account.boot.commons.exception.APIException;
import com.account.boot.commons.result.ResultCodeEnum;
import com.account.boot.commons.result.ResultDO;
import com.account.boot.interfaces.admin.AdminService;
import com.account.boot.model.vo.AdminVo;
import com.account.boot.service.admin.AdminMapperService;

@Service
public class AdminServiceImpl implements AdminService{

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private AdminMapperService adminMappingService;
	
	/**
	 * 登录
	 */
	public ResultDO<AdminVo> login(String name, String password) {
		ResultDO<AdminVo> resultDO = ResultDO.buildResult();
		try {
			AdminVo adminVo = adminMappingService.findByName(name);
			if(adminVo == null || !adminVo.getPassword().equals(password)) {
				throw new APIException(ResultCodeEnum.ADMIN_IS_NOT_USER);
			}
			resultDO.setModule(adminVo);
		} catch (APIException e) {
			logger.error("method:login name:" + name + " password:" + password);
			resultDO.setSuccess(false);
			resultDO.setResultCode(e.getResultCode());
			resultDO.setErrorMessage(e.getErrorMessage());
		} catch (Exception e) {
			logger.error("method:login name:" + name + " password:" + password);
			resultDO.setResultCode(ResultCodeEnum.UNKNOW.getResultCode());
			resultDO.setErrorMessage(ResultCodeEnum.UNKNOW.getMessage());
		}
		return resultDO;
	}
}
